<?php

namespace app\modules\hr\modules\staff\controllers;

use app\controllers\BaseController;
use app\data\StaffData;
use app\lib\tools\ExcelTools;

class DefaultController extends BaseController
{
	public  $currentMenu = 'staff';

	private $_staffManager;



	/**
	 * 初始化函数
	 */
	public function init(){
		parent::init();
		$this->_staffManager = \YII::$app->mydi->get("app\lib\logic\StaffLogicInterface");

	}

	/**
	 * 首页
	 * @return view
	 */
    public function actionIndex(){
    	return $this->render("index");
	}


	public function actionGetconstellation(){
        $date = \YII::$app->request->get("date");

        if(empty($date)) return null;

        return $this->renderJson(StaffData::matchConstellation($date));
    }

	public function actionAll(){
		$list = $this->_staffManager->getListAll(['position_status' => 1,'country_code'=>1],true);
		return $this->renderJson($list);
	}

	public function actionFoundation(){
		$id = \YII::$app->request->get("id");
		return $this->render('foundation',["id"=>$id,"foudationUrl"=>\YII::$app->params["foudationUrl"]]);
	}

	/**
	 * 修改页面
	 * @return view
	 */
	// public function actionEdit(){

	// 	$id = \YII::$app->request->get("id");

	// 	$oData = $this->_staffManager->getOneJoinAll($id);

	// 	return $this->render("edit",["oData"=>$oData]);
	// }
	// public function actionUpdate(){
	// 	$post = \YII::$app->request->post();
	// 	$result = $this->_staffManager->update($post);

	// 	if($result === false)
	// 		return $this->renderJson(["status"	=>	0,"info"	=>	"修改失败"]);

	// 	return $this->renderJson(["status"	=>	1,"info"	=>	"修改成功"]);
	// }


	//整合foudation的编辑
	public function actionEdit(){
		$id = \YII::$app->request->get("id");

		return $this->render("edit",["id"=>$id]);
	}

	public function actionDetail(){

		$id = \YII::$app->request->get("id");

		$data = $this->_staffManager->getOneJoinAll($id);

		$staff = (new \yii\db\Query())
	    ->from('cn_staff')
	    ->where(['id' => $data['superior_id']])
	    ->one();



	    $superior_name = empty($staff)?"":$staff['name_en_long'];


		return $this->render("detail",['data'=>StaffData::toShowModelData($data),"superior_name"=>$superior_name]);
	}


	public function actionOtheredit(){
		$id = \YII::$app->request->get("id");

		$oData = $this->_staffManager->getOneJoinAll($id);

		return $this->render("otheredit",["oData"=>$oData]);
	}
	public function actionUpdate(){
		$post = \YII::$app->request->post();

		$result = $this->_staffManager->updateOther($post);

		if($result == false)
			return $this->renderJson(["status"	=>	0,"info"	=>	"修改失败"]);

		return $this->renderJson(["status"	=>	1,"info"	=>	"修改成功"]);
	}



	/**
	 * 添加信息页面
	 * @return view
	 */
	public function actionAdd(){
		return $this->render("add");
	}

	public function actionInsert(){
		$post = \YII::$app->request->post();

		$result = $this->_staffManager->add($post);

		if($result === false)
			return $this->renderJson(["status"	=>	0,"info"	=>	"添加失败"]);

		return $this->renderJson(["status"	=>	1,"info"	=>	"添加成功"]);
	}

	/**
	 * 获取列表数据
	 * @return array
	 */
	public function actionList(){
		$request = \YII::$app->request;

		$aWhere = [
		    "and",
            ['or',
                ['=' ,"country_code",1],
                ['is' ,"country_code",null]
            ]
        ];


        $aWhere[] = ["in","department_id",\YII::$app->session->get($request->get("company")."_unit_id")];

		$list = $this->_staffManager->getConditionList(
            $aWhere,
			$request->get("skip"),
			$request->get("pageSize"),
			"created_date desc"
		);

		return $this->renderJson([
			"list"	=> StaffData::toListViewModel($list),
			"count"	=>	$this->_staffManager->getConditionCount($aWhere)
		]);
	}

	/**
	 * 删除证书记录
	 * @return array
	 */
	public function actionDelcertificate(){
		$id = \YII::$app->request->get("certificate_id");

		$result = $this->_staffManager->delCertificateById($id);

		if($result == false)
			return $this->renderJson(["status"=>0,"info"=>"删除失败"]);

		return $this->renderJson(["status"=>1,"info"=>"删除成功"]);
	}

	/**
	 * 删除教育记录
	 * @return json
	 */
	public function actionDeleducation(){
		$id = \YII::$app->request->get("education_id");

		$result = $this->_staffManager->delEducationById($id);

		if($result == false)
			return $this->renderJson(["status"=>0,"info"=>"删除失败"]);

		return $this->renderJson(["status"=>1,"info"=>"删除成功"]);
	}

	/**
	 * 获取所有在职员工
	 * @return [type] [description]
	 */
	public function actionGetstaffpositionstatusactive(){

		$olList = $this->_staffManager->getConditionList(
			[
				"and",
				["in","position_status" , [1,3]],
				[
					'or',
					['=' ,"country_code",1],
					['is' ,"country_code",null]
				]
			],

			null,
			null,
			"name_en_long asc",
			true
		);

		foreach ($olList as $key => $value){
		//	$olList[$key]['name_cn_long']  =  $olList[$key]['name_cn_long']."(".$olList[$key]['department_name'].")";
			$olList[$key]['name_en_long']  =  $olList[$key]['name_en_long'].$olList[$key]['name_cn_long']."(".$olList[$key]['department_name'].")";
		}

		return $this->renderJson($olList);
	}

	/**
	 * 根据职位获取在职员工
	 * @return json
	 */
	public function actionGetstaffselectbypositionid(){

		$positionId = \YII::$app->request->get("position_id");

		if(empty($positionId))
			return $this->renderJson([]);

		$list = $this->_staffManager->getListAll(
			[
				"position_status" => 1,
				"position_id" => $positionId
			]
			,true
		);
		return $this->renderJson(StaffData::toStaffSelectList($list));
	}


	/**
	 * 删除家庭成员记录
	 * @return json
	 */
	public function actionDelmember(){
		$id = \YII::$app->request->get("member_id");

		$result = $this->_staffManager->delMemberById($id);

		if($result == false)
			return $this->renderJson(["status"=>0,"info"=>"删除失败"]);

		return $this->renderJson(["status"=>1,"info"=>"删除成功"]);
	}

	/**
	 * 删除工作记录
	 * @return json
	 */
	public function actionDelwork(){
		$id = \YII::$app->request->get("work_id");

		$result = $this->_staffManager->delWorkById($id);

		if($result == false)
			return $this->renderJson(["status"=>0,"info"=>"删除失败"]);

		return $this->renderJson(["status"=>1,"info"=>"删除成功"]);
	}

	/**
	 * 导出excel筛选页面
	 * @return view
	 */
	public function actionExportview(){
		return $this->render("export");
	}


	/**
	 * 导出excel
	 * @return 下载
	 */
	public function actionExport(){
		$post = \YII::$app->request->post();
		if(!empty($post['start_date'])){
			if(strtotime($post['end_date']) < strtotime($post['start_date'])){
				return $this->render("index");
			}
		}

		$where = ["and"];

		if(!empty($post['department_id']))
			$where = ["and",['in','department_id',$post['department_id']]];

		if(!empty($post['name_cn_long']))
			$where[] = ['like', 'name_cn_long', $post['name_cn_long']];

		if(!empty($post['position_id']))
			$where[]=['in','position_id',$post['position_id']];

		if(!empty($post['start_date'])){
			$where[]=['>=','UNIX_TIMESTAMP(hired_date)',strtotime($post['start_date'])];
		}

		if( !empty($post['end_date'])){
			$where[]=['<=','UNIX_TIMESTAMP(hired_date)',strtotime($post['end_date']." 23:59:59")];
		}

		if(!empty($post['position_status_id'])){
			$where[]=["in","position_status",$post['position_status_id']];
		}

		if(!empty($post['start_dob'])){
			$where[]=["=","MONTH(dob)",$post['start_dob']];
		}


		//实习期
		if(!empty($post['start_internship'])){
			$where[]=[">=","internship_start_date",date("Y-m-d",strtotime($post['start_internship']))];
		}

		if(!empty($post['end_internship'])){
			$where[]=["<=","internship_end_date",date("Y-m-d 23:59:59",strtotime($post['end_internship']))];
		}

		//离期日期
		if(!empty($post['start_terminated'])){
			$where[]=[">=","terminated_date",date("Y-m-d",strtotime($post['start_terminated']))];
		}
		if(!empty($post['end_terminated'])){
			$where[]=["<=","terminated_date",date("Y-m-d 23:59:59",strtotime($post['end_terminated']))];
		}

		$where[] = ["or",["=","country_code",1],["is","country_code",null]];

		$list = $this->_staffManager->getListAll($where,true);
	
		ExcelTools::export(
			StaffData::toListViewModel($list),//数据
			[
									
                "serial_number",    "company_name",        "position_status",      "name_cn_long",                         "name_en_long",         "department_name",      "group_name",
                "position_name",   "superior_name",         "hired_date",           "work_place",                           "gender",               "dob",                  "identity_card_number",
                "contract_status",          "household_category",   "household_location",                   "bank_name",            "bank_number",          "phone_number",
                "education",                "graduation_school",    "major",                                "marital_status",       "finger_print_number",  "seat_number",
                "entrance_guard_number",    "emergency_contact",    "emergency_contact_phone_number",       "nation",               "political_status",     "email",
                "address1",          "current_address",            "terminated_date",       "real_birthday",   "constellation",               "comment"
			],//要导出的字段
			[
				 

                "档案编号",		"公司" ,         "岗位状态",         "中文名",           "英文名",          "部门",           "组别",
                "职位",    "直属上司",        "入职时间",         "办公地点",         "性别",            "出生日期",        "身份证号码",
                "合同状态",         "户口性质",         "户口所在地",       "银行类别",         "银行卡号",        "联系电话",
                "学历",            "毕业院校",         "专业",            "婚姻状态",         "指纹号",          "座位号",
                "门禁号",            "紧急联系人",       "紧急联系人电话",     "民族",            "政治面貌",        "邮件",
                "身份证地址",        "现居住地址",         "离职日期",   "真实生日",      "星座",      "经历描述"

			],//列名
			"员工信息" //文件名
		);
	}

}
